export default function createStore(reducer) {
    let currentState = null;
    let listeners = []
    const getState = () => {
        return currentState
    }
    const dispatch = (actions) => {
        currentState = reducer(currentState, actions)
        listeners.forEach(listener => listener())
    }
    const subscribe = (listener) => {
        listeners.push(listener)
    }
    dispatch({type: "KKBREDUX/OOOO"});
    // TODO do something
    return {
        getState,
        dispatch,
        subscribe
    }
}

// 多哥
export function combineReducers(reducers) {
    return function combination(state = {a:'as'}, action) {
        console.log(state)
        let nextState = {}
        let hasChanged = false;
        for (let key in reducers) {
            const reducer = reducers[key];
            nextState[key] = reducer(state[key], action)
            hasChanged = hasChanged || state[key] !== nextState[key]
        }
        hasChanged = hasChanged || Object.keys(nextState).length !== Object.keys(state).length;
        console.log(state,nextState)
        return hasChanged ? nextState : state
    }
}

